<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2017/2/23
 * Time: 8:59
 */

namespace Common\Model;


class GoodsModel extends BaseModel
{
    // 主键名称
    protected $pk = 'gid';
    // 数据表名（不包含表前缀）
    protected $tableName = 'goods';

    protected $_validate = array(
        array('gname','require','商品名称必须！'), //默认情况下用正则进行验证
        array('mprice','require','市场价必须！'), //默认情况下用正则进行验证
        array('xmprice','require','商城价必须！'), //默认情况下用正则进行验证
        array('ginventory','require','总库存必须！'), //默认情况下用正则进行验证
        array('glist','require','列表图必须！'), //默认情况下用正则进行验证
    );

    public function addGoods(){

        $editGid = I('get.gid');
        if(!$this->create()){
            return ['valid'=>false,'message'=>$this->getError()];
        }

        // 验证图册是否上传
        if(!isset($_POST['detailPic'])){
            return ['valid'=>false,'message'=>'请上传商品图册'];
        }

        $data['gname'] = I('post.gname');
        $data['gcode'] = I('post.gcode');
        $data['gunit'] = I('post.gunit');
        $data['mprice'] = I('post.mprice');
        $data['xmprice'] = I('post.xmprice');
        $data['ginventory'] = I('post.ginventory');
        $data['glist'] = I('post.glist');
        $data['gclick'] = I('post.gclick');
        $data['bid'] = I('post.bid');
        $data['cid'] = I('post.cid');
        $data['tid'] = I('post.tid');
//        dd($data);die;
        if($editGid){
            $data['gid'] = $editGid;
            $this->save($data);
        }else{
            $data['aid'] = $_SESSION['adminUsr']['aid'];//管理员id，取自于session
            $data['gptime'] = date("Y-m-d H:i:s",time());
            $gid = $this->add($data);//返回自增id
        }


        // 对上传的商品图册进行缩略处理，生成三张图片路径
        $small = '';
        $medium = '';
        $big = '';
        foreach(I('post.detailPic') as $k => $v){
            $big .= $v . '|';
            $image = new \Think\Image();
            $image->open($v);
            $smallPath = dirname($v) . '/small_'.basename($v);
            $small .= $smallPath . '|';
            $mediumPath = dirname($v) . '/medium_'.basename($v);
            $medium .= $mediumPath . '|';
            $image->thumb(48, 48)->save($smallPath);
            $image->thumb(450, 450)->save($mediumPath);
        }

        // 添加商品详情
        $data['small'] = rtrim($small,'|');
        $data['medium'] = rtrim($medium,'|');
        $data['big'] = rtrim($big,'|');
        $data['gdetails'] = I('post.gdetails');
        $data['cservice'] = I('post.cservice');
        if($editGid){
            $data['gid'] = $editGid;
            M('GoodsAttr')->where("gid='{$editGid}'")->delete();
            M('GoodsDetails')->where("gid='{$editGid}'")->save($data);
        }else{
            $data['gid'] = $gid;
            M('GoodsDetails')->add($data);
        }

//        dd($_POST);die;
        // 对属性进行循环添加
        foreach(I('post.attr') as $k => $v){
            $data['gattr'] = $v;
            $data['tpid'] = $k;
            $data['addprice'] = 0;
            M('GoodsAttr')->add($data);
        }

        // 对规格进行循环添加
        foreach(I('post.spec') as $k => $v){
            foreach($v['spec'] as $kk => $vv){
                $data['gattr'] = $vv;
                $data['tpid'] = $k;
                $data['addprice'] = $v['added'][$kk];
                M('GoodsAttr')->add($data);
            }
        }

        return ['valid'=>true,'message'=>'提交成功'];
    }
}